Code
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
kakamana
April 8, 2023
Convolutional neural networks gain a great deal of power when they are constructed with multiple layers (deep networks). You will learn how to stack multiple convolutional layers into a deep network in this chapter. As the network grows, you will also learn how to keep track of the number of parameters and how to control it.
This Going Deeper is part of Datacamp course: Image Processing with Keras in Python Deep learning methods are used in image processing to train neural network algorithms to perform various machine learning tasks. Using convolutional neural networks (CNNs), you will be able to classify different types of objects for the analysis of images. In this four-hour course, you will be taught how to construct, train, and evaluate CNNs using Keras.
It is a challenging task to turn images into data and train neural networks to classify them using deep learning. It has extensive applications in business and research ranging from helping eCommerce sites manage inventory more efficiently to assisting cancer researchers to identify dangerous melanoma quickly.
In the first chapter of this course, you will learn how images can be viewed as data and how you can use Keras to train a neural network to classify objects within an image.
A fundamental component of CNNs is convolutions, which will be discussed in the second chapter. Through the use of test data, you will learn how Keras CNNs operate on image data and how to train and tweak your Keras CNN. In later chapters, you will learn how to create a deep learning network in greater detail.
In this course, you will learn how to track the performance of a CNN and how to improve it. By this point, you will be able to create Keras neural networks, optimize them, and visualize their responses across a variety of applications.
This is my learning experience of data science through DataCamp. These repository contributions are part of my learning journey through my graduate program masters of applied data sciences (MADS) at University Of Michigan, DeepLearning.AI, Coursera & DataCamp. You can find my similar articles & more stories at my medium & LinkedIn profile. I am available at kaggle & github blogs & github repos. Thank you for your motivation, support & valuable feedback.
These include projects, coursework & notebook which I learned through my data science journey. They are created for reproducible & future reference purpose only. All source code, slides or screenshot are intellactual property of respective content authors. If you find these contents beneficial, kindly consider learning subscription from DeepLearning.AI Subscription, Coursera, DataCamp
Creating a deep learning network A deep convolutional neural network is a network that has more than one layer. Each layer in a deep network receives its input from the preceding layer, with the very first layer receiving its input from the images used as training or test data.
Here, you will create a network that has two convolutional layers.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten
img_rows, img_cols = 28, 28
model = Sequential()
# Add a convolutional layer (15 units)
model.add(Conv2D(15, kernel_size=2, input_shape=(img_rows, img_cols, 1), activation='relu'))
# Add another convolutional layer (5 units)
model.add(Conv2D(5, kernel_size=2, activation='relu'))
# Flatten and feed to output layer
model.add(Flatten())
model.add(Dense(3, activation='softmax'))
model.summary()
Metal device set to: Apple M2 Pro
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 27, 27, 15) 75
conv2d_1 (Conv2D) (None, 26, 26, 5) 305
flatten (Flatten) (None, 3380) 0
dense (Dense) (None, 3) 10143
=================================================================
Total params: 10,523
Trainable params: 10,523
Non-trainable params: 0
_________________________________________________________________
Training a deep learning model is very similar to training a single layer network. Once the model is constructed (as you have done in the previous exercise), the model needs to be compiled with the right set of parameters. Then, the model is fit by providing it with training data, as well as training labels. After training is done, the model can be evaluated on test data.
(train_data, train_labels), (test_data, test_labels) = tf.keras.datasets.fashion_mnist.load_data()
train_data = train_data[(train_labels >= 0) & (train_labels < 3)][0:50].reshape(-1, 28, 28, 1)
train_labels = train_labels[(train_labels >= 0) & (train_labels < 3)][0:50]
train_labels = pd.get_dummies(train_labels).to_numpy()
test_data = test_data[(test_labels >= 0) & (test_labels < 3)][0:10].reshape(-1, 28, 28, 1)
test_labels = test_labels[(test_labels >= 0) & (test_labels < 3)][0:10]
test_labels = pd.get_dummies(test_labels).to_numpy()
Epoch 1/3
4/4 [==============================] - 1s 43ms/step - loss: 27.9144 - accuracy: 0.3750 - val_loss: 3.2749 - val_accuracy: 0.6000
Epoch 2/3
4/4 [==============================] - 0s 14ms/step - loss: 6.7234 - accuracy: 0.7000 - val_loss: 2.3842e-08 - val_accuracy: 1.0000
Epoch 3/3
4/4 [==============================] - 0s 13ms/step - loss: 1.0786 - accuracy: 0.9500 - val_loss: 0.0061 - val_accuracy: 1.0000
1/1 [==============================] - 0s 15ms/step - loss: 0.0657 - accuracy: 1.0000
2023-04-08 23:51:04.303747: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz
[0.06566821038722992, 1.0]
We need to know how many parameters a CNN has, so we can adjust the model architecture, to reduce this number or shift parameters from one part of the network to another. How many parameters would a network have if its inputs are images with 28-by-28 pixels, there is one convolutional layer with 10 units kernels of 3-by-3 pixels, using zero padding (input has the same size as the output), and one densely connected layer with 2 units?
In this exercise, you will use Keras to calculate the total number of parameters along with the number of parameters in each layer of the network.
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 27, 27, 10) 50
conv2d_3 (Conv2D) (None, 26, 26, 10) 410
flatten_1 (Flatten) (None, 6760) 0
dense_1 (Dense) (None, 3) 20283
=================================================================
Total params: 20,743
Trainable params: 20,743
Non-trainable params: 0
_________________________________________________________________
As we have seen before, CNNs can have a lot of parameters. Pooling layers are often added between the convolutional layers of a neural network to summarize their outputs in a condensed manner, and reduce the number of parameters in the next layer in the network. This can help us if we want to train the network more rapidly, or if we don’t have enough data to learn a very large number of parameters.
A pooling layer can be described as a particular kind of convolution. For every window in the input it finds the maximal pixel value and passes only this pixel through. In this exercise, you will write your own max pooling operation, based on the code that you previously used to write a two-dimensional convolution operation.
AttributeError: 'DataFrame' object has no attribute 'dtype'
NameError: name 'imt' is not defined
def plot_comparison(img_original, img_filtered, img_title_filtered):
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 8))
ax1.imshow(img_original, cmap=plt.cm.gray)
ax1.set_title('Original')
ax1.axis('off')
ax2.imshow(img_filtered, cmap=plt.cm.gray)
ax2.set_title(img_title_filtered)
ax2.axis('off')
plot_comparison(imt, result, 'After MaxPooling')
NameError: name 'imt' is not defined
from tensorflow.keras.layers import MaxPool2D
model = Sequential()
# Add a convolutional layer
model.add(Conv2D(15, kernel_size=2, activation='relu', input_shape=(img_rows, img_cols, 1)))
# Add a pooling operation
model.add(MaxPool2D(2))
# Add another convolutional layer
model.add(Conv2D(5, kernel_size=2, activation='relu'))
# Flatten and feed to output layer
model.add(Flatten())
model.add(Dense(3, activation='softmax'))
model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_4 (Conv2D) (None, 27, 27, 15) 75
max_pooling2d (MaxPooling2D (None, 13, 13, 15) 0
)
conv2d_5 (Conv2D) (None, 12, 12, 5) 305
flatten_2 (Flatten) (None, 720) 0
dense_2 (Dense) (None, 3) 2163
=================================================================
Total params: 2,543
Trainable params: 2,543
Non-trainable params: 0
_________________________________________________________________
Keras implements a pooling operation as a layer that can be added to CNNs between other layers. In this exercise, you will construct a convolutional neural network similar to the one you have constructed before:
Convolution => Convolution => Flatten => Dense
However, you will also add a pooling layer. The architecture will add a single max-pooling layer between the convolutional layer and the dense layer with a pooling of 2x2:
Convolution => Max pooling => Convolution => Flatten => Dense
Epoch 1/3
4/4 [==============================] - 1s 41ms/step - loss: 6.0605 - accuracy: 0.2250 - val_loss: 3.5208 - val_accuracy: 0.4000
Epoch 2/3
4/4 [==============================] - 0s 13ms/step - loss: 2.3224 - accuracy: 0.6500 - val_loss: 1.6885 - val_accuracy: 0.6000
Epoch 3/3
4/4 [==============================] - 0s 13ms/step - loss: 0.9483 - accuracy: 0.8000 - val_loss: 1.1728 - val_accuracy: 0.8000
1/1 [==============================] - 0s 15ms/step - loss: 1.0369 - accuracy: 0.9000